class Compiler {
    constructor(vm) {
        this.el = vm.$el
        this.vm = vm
        this.compile(this.el)
    }
    compile(el) {
        const children = el.childNodes;
        Array.from(children).forEach(node=>{
            if(this.isTextNode(node)) {
                this.compileText(node)
            }else if(this.isElementNode(node)) {
                this.compileElem(node)
            }
            // 判断节点是否有字节点递归调用
            if (node.childNodes && node.childNodes.length) {
                this.compile(node);
            }
        })
    }
    compileText(node) {
        const reg = /\{\{(.+?)\}\}/
        const value = node.textContent
        if(reg.test(value)){
            const key = RegExp.$1.trim();
            node.textContent = value.replace(reg, this.vm[key]) 
            new Wacther(this.vm, key, (newValue)=>{
                node.textContent = value.replace(reg, newValue) 
            })
        }
    }
    compileElem(node) {
        const attrs = node.attributes
        Array.from(attrs).forEach(attr=> {
            let attrName = attr.name
            if(this.isDirective(attrName)) {
                attrName = attrName.replace('v-', '')
                const key = attr.value
                this.updater(node, attrName, key)
            }
        })
    }
    updater(node, attrName, key) {
        if( attrName.includes(":") ){
            attrName = attrName.substr(3)
            this.hanlder(node, attrName, key)
        } else {
            const updater = this[attrName + 'Updater']
            updater && updater.call(this, node, this.vm[key], key)
        }
       
    }
    hanlder(node, attrName, key){
        node.addEventListener(attrName, ()=>{
            let fnName = key
            let pramas
            const reg = /\((.+?)\)/
            if(reg.test(fnName)){
                fnName = fnName.replace(reg, '')
                pramas = this.handlerPramas(RegExp.$1)
            }
            const onHandler = this.vm.methods[fnName]
            onHandler && onHandler.apply(this.vm, pramas)
        })
    }
    handlerPramas(str){
        if(typeof str !== 'string'){
            return []
        }
        const pramas = str.split(',')
        return pramas.map(item=> {
            if(item.includes("'") || item.includes('"')){
                item = item.replace(/['|"]/g, '').trim()
            }else{
                item = this.vm[item]
            }
            return item
        })
    }
    modelUpdater(node, value, key) {
        node.value = value
        new Wacther(this.vm, key, (newValue)=>{
            node.value = newValue
        })
        node.addEventListener('input', ()=>{
            this.vm[key] = node.value;
        })   
    }
    htmlUpdater(node, value, key) {
        node.innerHTML = value
        new Wacther(this.vm, key, (newValue)=>{
            node.innerHTML = newValue
        })
    }
    isDirective(attrName) {
        return attrName.startsWith('v-')
    }
    isTextNode(node) {
        return node.nodeType === 3
    }
    isElementNode(node) {
        return node.nodeType === 1
    }
}